#pragma once
#include "Customer_Service.hpp"
#include "aisound.h"
#include "sound_to_str.h"
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <regex>
#include <thread>
#include <mutex>
#include <comdef.h> 
#include <future>
#include <boost/locale.hpp>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "msc_x64.lib") // x64
class Customer_Service_app
{
public:
    //主循环
    int exec() {
        SetConsoleOutputCP(CP_UTF8);
        try
        {
            //初始化设备
            boost::asio::io_context io_context;
            ssl::context context(ssl::context::sslv23);
            context.set_default_verify_paths();
            Customer_Service AI(io_context, context, "api.deepseek.com", "443");
            unsigned int playset = 0;
            bool is_quit = 0;
            
            //语音播报线程采用poll模式
            std::thread soundthread(
                [&is_quit, &playset]()
                {
                    while (!is_quit)
                    {
                        int i = 0;
                        while (playset)
                        {
                            if (playset & (1 << i))
                            {
                                char* p = new char[sizeof((std::string() + char('a' + i) + ".wav").c_str())];
                                strcpy(p, (std::string() + char('a' + i) + ".wav").c_str());
                                const size_t cSize = strlen(p) + 1;
                                wchar_t* wc = new wchar_t[cSize];
                                memset(wc, 0, cSize * sizeof(wchar_t));
                                mbstowcs(wc, p, cSize);
                                PlaySoundW(wc, NULL, SND_FILENAME | SND_SYNC);
                                playset &= ~(1 << i);
                                delete[] wc;
                                delete[] p;
                            }
                            i++;
                        }
                    }
                }
            );
            //上传语料
            std::cout << "按任意键开始麦克风，再次按任意键结束麦克风";
            AI.send();
            std::string str = AI.recv();
            std::string filename("a");
            aisound((filename + ".wav").c_str(), str.c_str());
            playset = 1;
            std::cout << "AI:" << str << std::endl;
            //语音听写 ai应答 语音合成
            while (true)
            {
             //语音听写sdk  
                std::string str;  
            t1:
                {
                auto future= std::async(sound_to_str);  //异步
                _getch();
                SetEvent(events[EVT_START]);
                _getch();
                SetEvent(events[EVT_QUIT]);
                PostQuitMessage(0);
            //ai应答
                str = future.get();
                }
                if(str.size()==0)goto t1;
                str = boost::locale::conv::to_utf<char>(str, "GBK");
                std::cout << "user:";
                std::cout << str << std::endl;
                if (str == "关闭终端。") return 0;
                AI.send(str);
                str = AI.recv();
                std::regex pattern(R"(\\n)");
                str = std::regex_replace(str, pattern, "\n");
             //语音合成
                filename.front()++;
                int i = (filename.front() - 'a') % 26;
                filename = i + 'a';
                aisound((filename + ".wav").c_str(), str.c_str());
                playset |= (1 << i);
                std::cout << "AI:" << str << std::endl;
            }
        }
        catch (std::exception& e) {
            std::cerr << "Exception: " << e.what() << "\n";
        }

        return 0;
    }
};